package com.example.demo.controller.surperController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.PartnerLogMapper;
import com.example.demo.base.Result;
import com.example.demo.entity.PartnerLog;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/partnerLogSurper")
public class PartnerLogSurperController {
    @Resource
    PartnerLogMapper partnerLogMapper;

    /**
     * 查询全部信息
     *
     * @return
     */
    @GetMapping("/all")
    public Result<?> findAll() {
        return Result.success(partnerLogMapper.selectList(null));
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/pageAll")
    public Result<?> findPageF(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<PartnerLog> wrapper = Wrappers.<PartnerLog>lambdaQuery();
        Page<PartnerLog> partnerLogPage = partnerLogMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(partnerLogPage);
    }

    /**
     * 根据物流公司代号查找
     * @param code
     * @return
     */
    @GetMapping("/findById/{code}")
    public Result<?> findByFarmId(@PathVariable String code) {
        PartnerLog partnerLog = partnerLogMapper.selectById(code);
        List<PartnerLog> partnerLogInfoList = new ArrayList<>();
        if (partnerLog != null) {
            partnerLogInfoList.add(partnerLog);
            return Result.success(partnerLogInfoList);
        }
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 根据物流公司名称模糊查找
     * @param name
     * @return
     */

    @GetMapping("/findByFirmlogName/{name}")
    public Result<?> findByProductName(@PathVariable String name) {
        LambdaQueryWrapper<PartnerLog> wrapper = Wrappers.<PartnerLog>lambdaQuery();
        wrapper.like(PartnerLog::getFirmlogName, name);
        List<PartnerLog> partnerLogInfoList = partnerLogMapper.selectList(wrapper);
        if (partnerLogInfoList.size() > 0)
            return Result.success(partnerLogInfoList);
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 删除信息
     * @param code
     * @return
     */

    @DeleteMapping("/delById/{code}")
    public Result<?> delete(@PathVariable String code) {
        int res = partnerLogMapper.deleteById(code);
        if (res > 0)
            return Result.success();
        return Result.error(0, "删除失败");
    }

    /**
     * 添加信息
     * @param firmId
     * @param firmlogName
     * @param respName
     * @param resTel
     * @param firmlogProfile
     * @param note
     * @return
     */
    @GetMapping(value = "/add")
    public Result<?> saveE(@RequestParam String firmId,
                           @RequestParam String firmlogName,
                           @RequestParam String respName,
                           @RequestParam String resTel,
                           @RequestParam String firmlogProfile,
                           @RequestParam String note,
                           @RequestParam String tenantId
    ) {
        if (firmId.isEmpty() || firmlogName.isEmpty() || respName.isEmpty() || resTel.isEmpty() || firmlogProfile.isEmpty()||
                note.isEmpty()) {
            return Result.error(0, "信息不能为空");
        }
        PartnerLog partnerLog = new PartnerLog(Integer.valueOf(firmId), firmlogName,respName, resTel,firmlogProfile,note,Integer.valueOf(tenantId));
        partnerLogMapper.insert(partnerLog);
        return Result.success();
    }

    @GetMapping(value = "/update")
    public Result<?> updateById(@RequestParam String firmId,
                           @RequestParam String firmlogName,
                           @RequestParam String respName,
                           @RequestParam String resTel,
                           @RequestParam String firmlogProfile,
                           @RequestParam String note,
                                @RequestParam String tenantId
    ) {
        if (firmId.isEmpty() || firmlogName.isEmpty() || respName.isEmpty() || resTel.isEmpty() || firmlogProfile.isEmpty()||
                note.isEmpty()) {
            return Result.error(0, "信息不能为空");
        }
        PartnerLog partnerLog = new PartnerLog(Integer.valueOf(firmId), firmlogName,respName, resTel,firmlogProfile,note,Integer.valueOf(tenantId));
        partnerLogMapper.updateById(partnerLog);
        return Result.success();
    }
}
